package com.gopay.bis.manager.bank.ebank;

import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.pab.ebank.PabB2bMessage;
import com.gopay.common.domain.bank.pab.ebank.PabQueryOrdersMessageRes;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

public class PabB2bManager implements BankManagerRemoteService {
	
	private BankService pabB2bBankServiceClientHna;
	
	private BankService pabB2bBankServiceClient;	
	
	@Resource(name = "pabConfig")
    private Map<String, String> pabConfig;
	
	@Override
	public Object sign(BaseMessage msg) throws GopayException {
		PabB2bMessage message = (PabB2bMessage)msg;
		message.setReturnUrl(message.getPayNotifyUrl());
		message.setNotifyUrl(message.getPayRuturnUrl());
		message.setBankUrl(message.getPayUrl());
		message.setMasterid(message.getMerID());
		message.setCurrency(pabConfig.get("PAB.CURTYPE.CNY"));
		message.setRemark("gopay");
		message.setValidtime("0");
		message.setMerchantName("guofubao");
		message.setPlainMessage(buildPlain(message));
		String signMessage = "";
		BankLogUtils.printB2BManagerSignSendLog(message);
		if(message.isHna()){
			signMessage = (String) pabB2bBankServiceClientHna.sign(message);
		}else{
			signMessage = (String) pabB2bBankServiceClient.sign(message);
		}
		message.setSignMessage(signMessage);
		BankLogUtils.printB2BManagerSignReturnLog(message);
		return message;
	}

	private String buildPlain(PabB2bMessage message) {
		StringBuffer orig = new StringBuffer("");
		orig.append("masterid=" + message.getMasterid() + "|");
		orig.append("orderid=" + message.getOrderId() + "|");
		orig.append("currency=" + message.getCurrency() + "|");
		orig.append("amount=" + message.getAmount().toString() + "|");
		orig.append("timestamp=" + message.getOrderDate() + "|");
		orig.append("validtime=" + message.getValidtime() + "|");
		orig.append("remark=" + message.getRemark() + "|");
		orig.append("merchantName=" + message.getMerchantName());
		return orig.toString();
	}

	@Override
	public Object verify(BaseMessage msg) throws GopayException {
		boolean flag = false;
		BankLogUtils.printB2BManagerVerifySendLog(msg);
		if(msg.isHna()){
			flag = (Boolean) pabB2bBankServiceClientHna.verify(msg);
		}else{
			flag = (Boolean) pabB2bBankServiceClient.verify(msg);
		}
		BankLogUtils.printB2BManagerVerifyReturnLog(msg, flag);
		return flag;
	}

	@Override
	public Object query(Object obj) throws GopayException {
		BankLogUtils.printB2BManagerQuerySendLog((BaseMessage)obj, "");
		PabQueryOrdersMessageRes orders;
		if(((BaseMessage)obj).isHna()){
			orders = (PabQueryOrdersMessageRes) pabB2bBankServiceClientHna.query(obj);
		}else{
			orders = (PabQueryOrdersMessageRes) pabB2bBankServiceClient.query(obj);
		}
		orders.setOutStlmCd(BankConstants.BANK_LOGO_PAB);
		BankLogUtils.printB2BManagerQueryReturnLog(orders);
		return orders;
	}

	public BankService getPabB2bBankServiceClient() {
		return pabB2bBankServiceClient;
	}

	public void setPabB2bBankServiceClient(BankService pabB2bBankServiceClient) {
		this.pabB2bBankServiceClient = pabB2bBankServiceClient;
	}

	public BankService getPabB2bBankServiceClientHna() {
		return pabB2bBankServiceClientHna;
	}

	public void setPabB2bBankServiceClientHna(BankService pabB2bBankServiceClientHna) {
		this.pabB2bBankServiceClientHna = pabB2bBankServiceClientHna;
	}

	public Map<String, String> getPabConfig() {
		return pabConfig;
	}

	public void setPabConfig(Map<String, String> pabConfig) {
		this.pabConfig = pabConfig;
	}

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
